package dshop.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Model;

import appbase.jfinal.ext.BaseModel;
import appbase.jfinal.ext.ModelKit;

public class Area extends BaseModel<Area>{
	
	private static final long serialVersionUID = -6967680902549117464L;
	
	public static final Area dao = new Area();

	public static <M extends Model<M>> void setNameToM(M m, String codeColumns, String newNameColumns) 
	{
		String[] columnArr = codeColumns.replace(" ", "").split(",");
		
		List<String> codeList = new ArrayList<String>();
		for (int i = 0; i < columnArr.length; i++) 
		{
			String code = m.getStr(columnArr[i]);
			if (code != null && !code.isEmpty())
			{
				codeList.add(code);
			}
		}
		
		if (codeList.isEmpty())
		{
			return;
		}
		
		String sql = " select `code`, full_name from area where `code` in (??) ";
		sql = dao.buildSqlHasInStrVals(sql, codeList);
		List<Area> areaList = dao.find(sql);
		Map<String, Area> areaMap = ModelKit.listToMap(areaList, "code", String.class);
		
		String[] newNameColumnArr = newNameColumns.replace(" ", "").split(",");
		
		for (int i = 0; i < columnArr.length; i++) 
		{
			String code = m.getStr(columnArr[i]);
			Area area = areaMap.get(code);
			if (area == null)
			{
				m.put(newNameColumnArr[i], "");
			}else 
			{
				m.put(newNameColumnArr[i], area.get("full_name"));
			}
		}
	}

	
}
